package com.meizi.mall.product.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.meizi.mall.product.dao.AttrGroupDao;
import com.meizi.mall.product.entity.AttrEntity;
import com.meizi.mall.product.entity.AttrGroupEntity;
import com.meizi.mall.product.service.AttrAttrgroupRelationService;
import com.meizi.mall.product.service.AttrGroupService;
import com.meizi.mall.product.service.AttrService;
import com.meizi.mall.product.utils.ProjectQueryWapper;
import com.meizi.mall.product.vo.AttrGroupWithAttrsVo;
import com.meizi.mall.product.vo.SpuItemAttrGroupVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meizi.core.tool.api.PageUtils;
import com.meizi.core.tool.api.Query;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
    @Autowired
    AttrAttrgroupRelationService relationService;
    @Autowired
    AttrService attrService;
    @Override
    public PageUtils queryPage(Map<String, Object> params, Long categoryId) {
//        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper();
//        queryWrapper.eq("catelog_id",categoryId);
//        String key = (String) params.get("key");
//        if(StringUtils.isNotBlank(key)){
//            queryWrapper.and((wrapper)->{
//                wrapper.eq("attr_group_id",key).or().like("attr_group_name",key);
//            });
//        }

        ProjectQueryWapper<AttrGroupEntity> attrEntityProjectQueryWapper = new ProjectQueryWapper<>();
        QueryWrapper<AttrGroupEntity> queryWrapper =
                attrEntityProjectQueryWapper.productQueryAttrByCommon(params, null, categoryId,"attr_group_id","attr_group_name");


        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils getNoRelationAttr(Map<String, Object> params, Long attrgroupId) {
        AttrGroupEntity attrGroupEntity = baseMapper.selectById(attrgroupId);
        Long catelogId = attrGroupEntity.getCatelogId();

        List<AttrGroupEntity> groups = baseMapper.selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        List<Long> groupids = groups.stream().map(AttrGroupEntity::getAttrGroupId).collect(Collectors.toList());


        List<Long> attrids = relationService.getRelationedAttrIdsBygroupIds(groupids);
        //查询category 下所有的attr
        ProjectQueryWapper<AttrEntity> attrEntityProjectQueryWapper = new ProjectQueryWapper<>();
        QueryWrapper<AttrEntity> queryWrapper = attrEntityProjectQueryWapper
                .productQueryAttrByCommon(params, "base", catelogId, "attr_id", "attr_name");
        queryWrapper.notIn("attr_id",attrids);

        IPage<AttrEntity> page = attrService.page(new Query<AttrEntity>().getPage(params), queryWrapper);

        return new PageUtils(page);
    }

    @Override
    public PageUtils getRelationAttr(Map<String, Object> params, Long attrgroupId) {
        ArrayList<Long> longs = new ArrayList<>();
        longs.add(attrgroupId);
        List<Long> attrids = relationService.getRelationedAttrIdsBygroupIds(longs);
        if(attrids!=null && attrids.size()>0){
            IPage<AttrEntity> page = attrService.page(new Query<AttrEntity>().getPage(params),
                    new QueryWrapper<AttrEntity>().in("attr_id",attrids));
            return new PageUtils(page);
        }else {
            return new PageUtils(new Page());
        }
    }

    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        //1、查询分组信息
        List<AttrGroupEntity> attrGroupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));

        //2、查询所有属性
        List<AttrGroupWithAttrsVo> collect = attrGroupEntities.stream().map(group -> {
            AttrGroupWithAttrsVo attrsVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(group,attrsVo);
            List<AttrEntity> attrs = attrService.getRelationAttr(attrsVo.getAttrGroupId());
            attrsVo.setAttrs(attrs);
            return attrsVo;
        }).collect(Collectors.toList());

        return collect;
    }

    @Override
    public List<SpuItemAttrGroupVo>  getAttrGroupWithAttrBySpuId(Long spuId, Long catalogId) {
        return baseMapper.getAttrGroupWithAttrBySpuId(spuId,catalogId);
    }

}